#!/bin/bash
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/sys_check.sh"
if [ -f /etc/os-release ]; then
   echo "Linux system."
elif [ -f /System/Library/CoreServices/SystemVersion.plist ]; then
   echo "macOS system."
   source ~/.bash_profile
elif current_os_is_windows; then
   echo "Windows system."
else
   echo "Unknown OS"
fi

# cleanup() {
#     echo "pressed Ctrl+C. exiting..."
#     exit 0
# }

# trap cleanup INT


APP_INSTALL_NAME="thinksound"
APP_TARGET_DIR_NAME="thinksound"

echo "lmdprogress:3"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_conda.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/conda_run.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/conda_and_pip_settings.sh"

source "$LMD_BASE_INSTALL_SCRIPT_DIR/apps/$APP_INSTALL_NAME/env"

export CURRENT_APP_PATH="${LMD_APPS_DIR}/${APP_TARGET_DIR_NAME}"
echo $CURRENT_APP_PATH
mkdir -p "$CURRENT_APP_PATH"

echo "lmdprogress:8"

if [ -d "$CURRENT_APP_PATH" ]; then
    echo "App Dir: "$CURRENT_APP_PATH
else
    echo "Error, App Dir dose not exist, please retry. "$CURRENT_APP_PATH
    exit 1
fi

echo "lmdprogress:12"

export GIT_HOST=${GITHUB_PROXY}"https://github.com"
export GIT_REPO_URL=${GIT_HOST}/FunAudioLLM/ThinkSound

export GIT_REPO_BRANCH="master"
export LOCAL_REPO_PATH="$CURRENT_APP_PATH"
sh "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/git_clone_and_fetch.sh"

echo "lmdprogress:20"

source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/conda_and_pip_settings.sh"

conda --version
echo "lmdprogress:22"
cd "$CURRENT_APP_PATH"
# CONDA_ENV_NAME=$APP_INSTALL_NAME
CONDA_PREFIX="./venv"

if [ -d "$CONDA_PREFIX" ]; then
    echo "found ./venv, remove it"
    rm -rf "$CONDA_PREFIX"
fi

export PYTHON_VER_ENV="3.10"
conda_create_env $CONDA_PREFIX python=$PYTHON_VER_ENV pip=25.2 -y

echo "lmdprogress:25"

cd "$CURRENT_APP_PATH"


install_torch() {
  if [ -f /System/Library/CoreServices/SystemVersion.plist ]; then
      conda_run_pip_install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/nightly/cpu
      source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_torch_mac.sh"
  elif current_os_is_windows; then
      source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_torch_multi_method.sh"
  fi
}

install_torch

if [ -f /System/Library/CoreServices/SystemVersion.plist ]; then

    conda_run_pip_install accelerate==1.9.0 decorator==5.2.1 gradio==3.50.2 gradio_client huggingface-hub==0.33.4 hf_xet laion-clap==1.1.7 moviepy==2.2.1 numpy==1.26.4 eva-decord==0.6.1 aeiou==0.0.20 sympy==1.13.3

    grep -vE '^torch==|torchvision==|torchaudio==|accelerate==|aeiou==|decorator==|flash-attn|gradio==|gradio_client==|huggingface-hub==|laion-clap==|moviepy==|numpy==|triton==|torchcubicspline|decord==|sympy==' requirements.txt > requirements-temp.txt

elif current_os_is_windows; then
    
    conda_run_pip_install accelerate==1.9.0 decorator==5.2.1 gradio==3.50.2 gradio_client huggingface-hub==0.33.4 hf_xet laion-clap==1.1.7 moviepy==2.2.1 numpy==1.26.4 triton-windows aeiou==0.0.20
    grep -vE '^torch==|torchvision==|torchaudio==|accelerate==|aeiou==|decorator==|flash-attn|gradio==|gradio_client==|huggingface-hub==|laion-clap==|moviepy==|numpy==|triton==|torchcubicspline|sympy==' requirements.txt > requirements-temp.txt

fi
echo "lmdprogress:35"
conda_run_conda_forge_install ffmpeg

echo "lmdprogress:45"
 
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_flash_attn.sh"

conda_run_pip_install --no-deps "git+${GIT_HOST}/patrick-kidger/torchcubicspline.git"

conda_run_pip_install -r requirements-temp.txt


# echo "lmdprogress:50"
# IS_CUDA_AVAILABLE=$(python -c "import torch; print(torch.cuda.is_available())" 2>/dev/null)

# if [ $? -ne 0 ] || [ "$IS_CUDA_AVAILABLE" != "True" ]; then
#     echo "CUDA 版本的 PyTorch 未安装，正在安装..."
#     pip uninstall torch torchvision torchaudio
#     install_torch
# else
#     echo "检测到已安装 CUDA 版本的 PyTorch。"
# fi

echo "lmdprogress:60"

if [ -z "$HF_ENDPOINT" ] && [ -n "$HF_MIRROR" ]; then
    export HF_ENDPOINT="$HF_MIRROR"
fi
echo $HF_ENDPOINT

echo "start download models"

export HF_HOME="./tmp_hf_home"
export U2NET_HOME="./unet_home"
export TORCH_HOME="./tmp_torch_home"

if [ -n "$GITHUB_PROXY" ]; then
  conda_run_cmd modelscope download --model 'iic/ThinkSound' --local_dir 'ckpts' --exclude '*.png'
else
  conda_run_hf_download FunAudioLLM/ThinkSound --repo-type model --revision main  --local-dir "ckpts" --exclude "*.png"
fi

echo "lmdprogress:70"
# --local-dir-use-symlinks False
conda_run_hf_download google/t5-v1_1-xl --exclude "*_model.h5"

echo "lmdprogress:80"
conda_run_hf_download facebook/metaclip-h14-fullcc2.5b


echo "lmdprogress:100"


